package com.android.exchangeapp;

import static com.android.exchangeapp.ExtsKt.applyStatusBar;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.WindowInsetsControllerCompat;

import com.android.exchangeapp.databinding.SecondWebPageActivityBinding;
import com.android.exchangeapp.modle.JumpInfo;

public class SecondWebPageActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private SecondWebPageActivityBinding mBinding;

    private String mUrl = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        JumpInfo jumpInfo = PageManager.INSTANCE.getJumpInfo();
        if (jumpInfo != null) {
            applyStatusBar(this, jumpInfo);
        }
        mBinding = SecondWebPageActivityBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        getWindow().setStatusBarColor(Color.BLACK);
        // getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        mUrl = getIntent().getStringExtra("url");
        setWebView(mBinding.webView);
    }

    private void setWebView(WebView webView) {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setSupportMultipleWindows(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setDisplayZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setBlockNetworkImage(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onLoadResource(WebView view, String url) {
                if (!PageManager.INSTANCE.onLoadResource(SecondWebPageActivity.this, view, url)){
                    super.onLoadResource(view, url);
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return PageManager.handleUri(SecondWebPageActivity.this, request.getUrl(), view);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try {
                    return PageManager.handleUri(SecondWebPageActivity.this, Uri.parse(url), view);
                } catch (Exception e) {
                    return false;
                }
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
                WebView newWebView = new WebView(view.getContext());
                newWebView.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        Log.e(TAG, "shouldOverrideUrlLoading: " + url);
                        startActivity(new Intent(SecondWebPageActivity.this, SecondWebPageActivity.class)
                                .putExtra("url", url));
                        return true;
                    }

                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                        startActivity(new Intent(SecondWebPageActivity.this, SecondWebPageActivity.class)
                                .putExtra("url", request.getUrl()));
                        return true;
                    }
                });
                WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
                transport.setWebView(newWebView);
                resultMsg.sendToTarget();
                return true;
            }
        });
//        Toast.makeText(this, mUrl, Toast.LENGTH_SHORT).show();
        webView.loadUrl(mUrl);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mBinding.webView.canGoBack()) {
                mBinding.webView.goBack();
            } else {
                mBinding.webView.loadUrl("auto:about");
                mBinding.webView.stopLoading();
                mBinding.webView.destroy();
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}